package com.zenithmind.version.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.version.pojo.dto.CommitCreateDTO;
import com.zenithmind.version.pojo.query.CommitQueryDTO;
import com.zenithmind.version.pojo.vo.CommitVO;
import com.zenithmind.version.service.CommitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 提交管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "提交管理", description = "提交管理相关接口")
@RestController
@RequestMapping("/api/v1/commits")
@RequiredArgsConstructor
public class CommitController {

    private final CommitService commitService;

    @Operation(summary = "创建提交", description = "创建新的提交")
    @PostMapping
    public Result<CommitVO> createCommit(@Valid @RequestBody CommitCreateDTO createDTO) {
        CommitVO commit = commitService.createCommit(createDTO);
        return Result.success(commit);
    }

    @Operation(summary = "分页查询提交", description = "分页查询提交列表")
    @PostMapping("/page")
    public Result<IPage<CommitVO>> getCommitPage(@Valid @RequestBody CommitQueryDTO queryDTO) {
        IPage<CommitVO> page = commitService.getCommitPage(queryDTO);
        return Result.success(page);
    }

    @Operation(summary = "获取提交详情", description = "获取提交详细信息")
    @GetMapping("/{id}")
    public Result<CommitVO> getCommitDetail(@Parameter(description = "提交ID") @PathVariable String id) {
        CommitVO commit = commitService.getCommitDetail(id);
        return Result.success(commit);
    }

    @Operation(summary = "根据哈希获取提交", description = "根据提交哈希获取提交信息")
    @GetMapping("/repository/{repositoryId}/hash/{commitHash}")
    public Result<CommitVO> getCommitByHash(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "提交哈希") @PathVariable String commitHash) {
        CommitVO commit = commitService.getCommitByHash(repositoryId, commitHash);
        return Result.success(commit);
    }

    @Operation(summary = "获取仓库提交", description = "获取仓库的提交历史")
    @GetMapping("/repository/{repositoryId}")
    public Result<List<CommitVO>> getRepositoryCommits(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "分支名称") @RequestParam(required = false) String branchName,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        List<CommitVO> commits = commitService.getRepositoryCommits(repositoryId, branchName, limit);
        return Result.success(commits);
    }

    @Operation(summary = "获取分支提交", description = "获取分支的提交历史")
    @GetMapping("/repository/{repositoryId}/branch/{branchName}")
    public Result<List<CommitVO>> getBranchCommits(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "分支名称") @PathVariable String branchName,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        List<CommitVO> commits = commitService.getBranchCommits(repositoryId, branchName, limit);
        return Result.success(commits);
    }

    @Operation(summary = "获取用户提交", description = "获取用户的提交历史")
    @GetMapping("/user/{authorId}")
    public Result<List<CommitVO>> getUserCommits(
            @Parameter(description = "作者ID") @PathVariable String authorId,
            @Parameter(description = "仓库ID") @RequestParam(required = false) String repositoryId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        List<CommitVO> commits = commitService.getUserCommits(authorId, repositoryId, limit);
        return Result.success(commits);
    }

    @Operation(summary = "获取我的提交", description = "获取当前用户的提交历史")
    @GetMapping("/my")
    public Result<List<CommitVO>> getMyCommits(
            @Parameter(description = "仓库ID") @RequestParam(required = false) String repositoryId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        String userId = UserContext.getUserId();
        List<CommitVO> commits = commitService.getUserCommits(userId, repositoryId, limit);
        return Result.success(commits);
    }

    @Operation(summary = "获取提交变更", description = "获取提交的文件变更")
    @GetMapping("/{id}/changes")
    public Result<List<Object>> getCommitChanges(@Parameter(description = "提交ID") @PathVariable String id) {
        List<Object> changes = commitService.getCommitChanges(id);
        return Result.success(changes);
    }

    @Operation(summary = "获取父提交", description = "获取提交的父提交")
    @GetMapping("/{id}/parents")
    public Result<List<CommitVO>> getParentCommits(@Parameter(description = "提交ID") @PathVariable String id) {
        List<CommitVO> parents = commitService.getParentCommits(id);
        return Result.success(parents);
    }

    @Operation(summary = "获取子提交", description = "获取提交的子提交")
    @GetMapping("/{id}/children")
    public Result<List<CommitVO>> getChildCommits(@Parameter(description = "提交ID") @PathVariable String id) {
        List<CommitVO> children = commitService.getChildCommits(id);
        return Result.success(children);
    }

    @Operation(summary = "比较提交", description = "比较两个提交的差异")
    @GetMapping("/compare")
    public Result<Object> compareCommits(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "源提交") @RequestParam String fromCommit,
            @Parameter(description = "目标提交") @RequestParam String toCommit) {
        Object differences = commitService.compareCommits(repositoryId, fromCommit, toCommit);
        return Result.success(differences);
    }

    @Operation(summary = "获取提交区间", description = "获取两个提交之间的提交列表")
    @GetMapping("/between")
    public Result<List<CommitVO>> getCommitsBetween(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "源提交") @RequestParam String fromCommit,
            @Parameter(description = "目标提交") @RequestParam String toCommit) {
        List<CommitVO> commits = commitService.getCommitsBetween(repositoryId, fromCommit, toCommit);
        return Result.success(commits);
    }

    @Operation(summary = "搜索提交", description = "根据关键词搜索提交")
    @GetMapping("/search")
    public Result<List<CommitVO>> searchCommits(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "分支名称") @RequestParam(required = false) String branchName,
            @Parameter(description = "作者ID") @RequestParam(required = false) String authorId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<CommitVO> commits = commitService.searchCommits(repositoryId, keyword, branchName, authorId, limit);
        return Result.success(commits);
    }

    @Operation(summary = "获取提交统计", description = "获取提交统计信息")
    @GetMapping("/repository/{repositoryId}/statistics")
    public Result<Object> getCommitStatistics(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "分支名称") @RequestParam(required = false) String branchName) {
        Object statistics = commitService.getCommitStatistics(repositoryId, branchName);
        return Result.success(statistics);
    }

    @Operation(summary = "获取用户统计", description = "获取用户提交统计")
    @GetMapping("/user/{authorId}/statistics")
    public Result<Object> getUserCommitStatistics(
            @Parameter(description = "作者ID") @PathVariable String authorId,
            @Parameter(description = "仓库ID") @RequestParam(required = false) String repositoryId) {
        Object statistics = commitService.getUserCommitStatistics(authorId, repositoryId);
        return Result.success(statistics);
    }

    @Operation(summary = "获取提交活动", description = "获取提交活动统计")
    @GetMapping("/repository/{repositoryId}/activity")
    public Result<Object> getCommitActivity(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate) {
        Object activity = commitService.getCommitActivity(repositoryId, startDate, endDate);
        return Result.success(activity);
    }

    @Operation(summary = "回滚提交", description = "回滚指定提交")
    @PostMapping("/{id}/revert")
    public Result<CommitVO> revertCommit(
            @Parameter(description = "提交ID") @PathVariable String id,
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "分支名称") @RequestParam String branchName) {
        String userId = UserContext.getUserId();
        CommitVO commit = commitService.revertCommit(repositoryId, id, branchName, userId);
        return Result.success(commit);
    }

    @Operation(summary = "挑选提交", description = "挑选提交到目标分支")
    @PostMapping("/{id}/cherry-pick")
    public Result<CommitVO> cherryPickCommit(
            @Parameter(description = "提交ID") @PathVariable String id,
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "目标分支") @RequestParam String targetBranch) {
        String userId = UserContext.getUserId();
        CommitVO commit = commitService.cherryPickCommit(repositoryId, id, targetBranch, userId);
        return Result.success(commit);
    }

    @Operation(summary = "获取提交图谱", description = "获取提交图谱")
    @GetMapping("/repository/{repositoryId}/graph")
    public Result<Object> getCommitGraph(
            @Parameter(description = "仓库ID") @PathVariable String repositoryId,
            @Parameter(description = "分支名称") @RequestParam(required = false) String branchName,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "100") Integer limit) {
        Object graph = commitService.getCommitGraph(repositoryId, branchName, limit);
        return Result.success(graph);
    }

    @Operation(summary = "验证提交哈希", description = "验证提交哈希是否有效")
    @GetMapping("/validate-hash")
    public Result<Boolean> validateCommitHash(
            @Parameter(description = "仓库ID") @RequestParam String repositoryId,
            @Parameter(description = "提交哈希") @RequestParam String commitHash) {
        Boolean isValid = commitService.validateCommitHash(repositoryId, commitHash);
        return Result.success(isValid);
    }

    @Operation(summary = "获取提交签名", description = "获取提交签名信息")
    @GetMapping("/{id}/signature")
    public Result<Object> getCommitSignature(@Parameter(description = "提交ID") @PathVariable String id) {
        Object signature = commitService.getCommitSignature(id);
        return Result.success(signature);
    }
}
